package com.hxkj.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.LeafletDao;
import com.hxkj.domain.enums.LeafletExceptionEnum;
import com.hxkj.domain.po.Leaflet;
import com.hxkj.service.LeafletService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章实现类
 * <p>
 * created by zzx on 2018/6/6.
 */
@Service
@Slf4j
public class LeafletServiceImpl implements LeafletService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LeafletServiceImpl.class);

    @Autowired
    private LeafletDao leafletDao;

    @Override
    public PageInfo<Leaflet> getLeafletList(Leaflet leaflet, PageTo pageParam) throws Exception {
        Page<Leaflet> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

        List<Leaflet> artList = new ArrayList<>();
        try {
            //查询 单页列表
            artList = leafletDao.getLeafletList(leaflet);
            PageInfo<Leaflet> pageInfo = new PageInfo<>(page);
            pageInfo.setList(artList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("查询单页列表错误");
        }
        return new PageInfo<>(page);
    }

    @Override
    public ResultDataStruct getLeaflet(Leaflet leaflet) throws Exception {
        ResultDataStruct<Leaflet> result = new ResultDataStruct<>();
        List<Leaflet> lea = new ArrayList<>();
        try {
            //查询一条单页信息
            lea = leafletDao.get(leaflet);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(LeafletExceptionEnum.LEALFET_GET_ERRO.getCode());
            result.setMessage(LeafletExceptionEnum.LEALFET_GET_ERRO.getMsg());
            return result;
        }
        if (lea.size() > 0) {
            result.setData(lea.get(0));
        }
        return result;
    }

    @Override
    public ResultMessageStruct updateLeaflet(Leaflet leaflet) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {

            //判断标题是否重复，不同类型的可以重复，相同类型得不能重复
            List<Leaflet> artList = leafletDao.getLeafletHeadline(leaflet);
            if (artList.size() > 0) {
                //如果同一类型的标题重复，则抛出 单页标题重复
                result.setCode(LeafletExceptionEnum.LEALFET_REPETITION.getCode());
                result.setMessage(LeafletExceptionEnum.LEALFET_REPETITION.getMsg());
                return result;
            }
            //修改一条单页信息
            leafletDao.update(leaflet);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(LeafletExceptionEnum.LEALFET_MODIFY_ERRO.getCode());
            result.setMessage(LeafletExceptionEnum.LEALFET_MODIFY_ERRO.getMsg());
            return result;
        }
        result.setCode(200);
        return result;
    }

    @Override
    public ResultMessageStruct removerLeaflet(Leaflet leaflet) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        //解析 leafletIds 单页id String数组
        String[] leafletIds = leaflet.getLeafletIds().split(",");
        try {
            //遍历单页id数组
            for (String leafletId : leafletIds) {
                Leaflet art = new Leaflet();
                art.setLeafletId(Long.valueOf(leafletId));
                //批量删除单页
                leafletDao.delete(art);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(LeafletExceptionEnum.LEAFLETEXCEPTIONENUM.getCode());
            result.setMessage(LeafletExceptionEnum.LEAFLETEXCEPTIONENUM.getMsg());
            return result;
        }
        return result;
    }

    @Override
    public ResultDataStruct saveLeaflet(Leaflet leaflet) throws Exception {
        ResultDataStruct<Leaflet> result = new ResultDataStruct<>();
        try {
            //判断标题是否重复，不同类型的可以重复，相同类型得不能重复
            List<Leaflet> artList = leafletDao.getLeafletHeadline(leaflet);
            if (artList.size() > 0) {
                //如果同一类型的标题重复，则抛出 单页标题重复
                result.setCode(LeafletExceptionEnum.LEALFET_REPETITION.getCode());
                result.setMessage(LeafletExceptionEnum.LEALFET_REPETITION.getMsg());
                return result;
            }
            //新增一条单页信息
            leafletDao.save(leaflet);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(LeafletExceptionEnum.LEALFET_SAVE_ERRO.getCode());
            result.setMessage(LeafletExceptionEnum.LEALFET_SAVE_ERRO.getMsg());
            return result;
        }
        result.setData(leaflet);
        return result;
    }

    @Override
    public ResultDataStruct getLeafletByStatus(Leaflet leaflet) throws Exception {
        ResultDataStruct<Leaflet> result = new ResultDataStruct<>();
        List<Leaflet> lea = new ArrayList<>();
        try {
            //查询一条启用的单页信息
            lea = leafletDao.getLeafletByStatus(leaflet);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(LeafletExceptionEnum.LEALFET_GET_ERRO.getCode());
            result.setMessage(LeafletExceptionEnum.LEALFET_GET_ERRO.getMsg());
            return result;
        }
        if (lea.size() > 0) {
            result.setData(lea.get(0));
        }
        return result;
    }

    /**
     * 获取所有启用的单页信息
     *
     * @param leaflet
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<List<Leaflet>> getLeafletList(Leaflet leaflet) throws Exception {
        ResultDataStruct<List<Leaflet>> listResultDataStruct = new ResultDataStruct<List<Leaflet>>();
        try {
            List<Leaflet> leafletList = leafletDao.get(leaflet);
            listResultDataStruct.setData(leafletList);
            listResultDataStruct.setCode(null);
        } catch (Exception e) {
            e.printStackTrace();
            listResultDataStruct.setCode(LeafletExceptionEnum.LEALFET_GET_ERRO.getCode());
            listResultDataStruct.setMessage(LeafletExceptionEnum.LEALFET_GET_ERRO.getMsg());
            return listResultDataStruct;
        }
        return listResultDataStruct;
    }


    @Override
    public ResultDataStruct<List<Leaflet>> getLeafletByIds(Leaflet leaflet) throws Exception {
        ResultDataStruct<List<Leaflet>> listResultDataStruct = new ResultDataStruct<List<Leaflet>>();
        try {
            List<Leaflet> leafletList = leafletDao.getLeafletByIds(leaflet);
            listResultDataStruct.setData(leafletList);
            listResultDataStruct.setCode(null);
        } catch (Exception e) {
            e.printStackTrace();
            listResultDataStruct.setCode(LeafletExceptionEnum.LEALFET_GET_ERRO.getCode());
            listResultDataStruct.setMessage(LeafletExceptionEnum.LEALFET_GET_ERRO.getMsg());
            return listResultDataStruct;
        }
        return listResultDataStruct;
    }

    /**
     * chenfangzhou
     *
     * @param leaflet
     * @return
     * @throws Exception
     */
    @Override
    public Leaflet getLeafletByPrimary(Leaflet leaflet) throws Exception {
        return leafletDao.get(leaflet.getLeafletId());
    }
}
